package com.camel.docker.task;

import com.camel.docker.config.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.io.input.TailerListenerAdapter;

import java.io.File;

@Slf4j
public class LogFileTailerTask extends Thread {
    private WebSocketServer webSocketServer;
    private String filePath;

    public LogFileTailerTask(WebSocketServer webSocketServer, String path) {
        this.webSocketServer = webSocketServer;
        this.filePath = path;
    }

    @Override
    public void run() {
        TailerListener listener = new TailerListenerAdapter() {
            @Override
            public void handle(String line) {
                try {
                    log.info("日志新增的内容为：" + line);
                    webSocketServer.sendInfo("999999", line);
                } catch (Exception ex) {
                    log.error("发生异常：" + ex.getMessage());
                }
            }
        };
        Tailer tailer = new Tailer(new File(filePath), listener, 1000, true);
        tailer.run();
    }
}
